# ~*~ coding: utf-8 ~*~
from flask import request, views, render_template, jsonify
from flask_login import current_user

from apps.extensions import db
from apps.perms.mixin import PermissionRequiredMixin, LoginRequiredMixin, LoginRequiredAndCsrfExemptMixin
from apps.assets.models import AssetPermissionRelated, Asset, SystemUser

__all__ = ['SSHConnectorView', 'TreeNodeListAPI']


class SSHConnectorView(LoginRequiredMixin, PermissionRequiredMixin, views.MethodView):
    permission_required = 'terminal-ssh-connector'

    def get(self):
        return render_template('terminal/ssh_terminal.html')


class TreeNodeListAPI(LoginRequiredAndCsrfExemptMixin, PermissionRequiredMixin, views.MethodView):
    """左边树形结构"""
    permission_required = 'terminal-ssh-connector'

    def get(self):
        """
        # default
        [{ id:'01',	name:'n1',	isParent:true},
        id:'02',	name:'n2',	isParent:false},
        id:'03',	name:'n3',	isParent:true},
        id:'04',	name:'n4',	isParent:false}]

        # id: 01     n: n1     lv: 0
        [{ id:'011',	name:'n1.n1',	isParent:true},
        { id:'012',	name:'n1.n2',	isParent:false},
        { id:'013',	name:'n1.n3',	isParent:true},
        { id:'014',	name:'n1.n4',	isParent:false}]


        # id: 011    n: n1.1   lv: 1
        [{ id:'0111',	name:'n1.1.n1',	isParent:true},
        { id:'0112',		name:'n1.1.n2',	isParent:false},
        { id:'0113',		name:'n1.1.n3',	isParent:true},
        { id:'0114',		name:'n1.1.n4',	isParent:false}]


        # id: 0111  n: n1.1.1   lv: 2
        [{ id:'01111',	name:'n1.1.1.n1',	isParent:false},
        { id:'01112',	name:'n1.1.1.n2',	isParent:false},
        { id:'01113',	name:'n1.1.1.n3',	isParent:false},
        { id:'01114',	name:'n1.1.1.n4',	isParent:false}]

        :return:
        """

        id_ = request.form.get('id')
        name = request.form.get('n')
        level = request.form.get('lv')

        if not level:
            return jsonify([{'id': '01', 'name': 'Home', 'isParent': True}, ])

        # 获取所以的主机
        if level == '0':
            aprs = db.session.query(AssetPermissionRelated.asset_id.distinct(), Asset). \
                join(Asset, Asset.id == AssetPermissionRelated.asset_id). \
                filter(AssetPermissionRelated.user_id == current_user.id). \
                all()

            user_assets = []
            for k, asset in aprs:
                user_assets.append({'id': asset.id, 'name': asset.hostname, 'isParent': True})
            return jsonify(user_assets)

        # 获取所以的系统用户
        if level == '1':
            aprs = db.session.query(AssetPermissionRelated.system_user_id, SystemUser). \
                join(SystemUser, SystemUser.id == AssetPermissionRelated.system_user_id). \
                filter(AssetPermissionRelated.user_id == current_user.id,
                       AssetPermissionRelated.asset_id == id_). \
                all()
            asset_system_users = []
            for k, system_user in aprs:
                asset_system_users.append({'id': system_user.id, 'name': system_user.username, 'isParent': False})
            return jsonify(asset_system_users)

    def post(self):
        return self.get()
